$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/product/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 80 },
			{ label: '标题(大字)', name: 'title', index: 'title', width: 120 },
			{ label: '副标题1', name: 'subtitle1', index: 'subtitle1', width: 100 },
			{ label: '副标题2', name: 'subtitle2', index: 'subtitle2', width: 80 },
			{ label: '图标', name: 'urlSmall', index: 'url_small', width: 80 ,formatter: function(value, options, row){
					var title = "图标";
					return "<img  style='width:30px;height: 30px;cursor:pointer;'  src='"+value+"' " +
						"onclick='showImg(\""+value+"\",\""+title+"\")'></img>";
				}
			},
			{ label: '人民币价格', name: 'priceCny', index: 'price_cny', width: 80 },
			{ label: 'Usdt价格', name: 'priceUsdt', index: 'price_usdt', width: 80 },
			{ label: '产出时长', name: 'times', index: 'times', width: 80 },
			{ label: '收益率', name: 'rate', index: 'rate', width: 80 },
			{ label: '是否显示', name: 'status', index: 'status', width: 80 , formatter: function(value, options, row){
					if(!value && value!=0){
						return '未知'
					}
				var g = {
						0 :"否", 1:"是"
					}
					if(!g[value]){
						return '未知'
					}
					return g[value];
				}
			},
			{ label: '用户最大下单量', name: 'active', index: 'active', width: 120 },

			{ label: '排序', name: 'sort', index: 'sort', width: 80 },
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 150 },
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 150 }
		],
		viewrecords: true,
        height: 480,
		scrollOffset:0,
        width: 1000,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		shrinkToFit:false,
		autoScroll: true,
		pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
		beforeProcessing: function(data, status, xhr) {
			// 假设我们检查名为"errorField"的字段
			if (data.code !== 0) {
				// 如果满足条件，则显示错误消息
				alert(data.msg);
				// 停止处理，不显示数据
				return false;
			}
			// 如果没有错误，继续处理数据
			return true;
		},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

function showImg(val,tit){
	$("#showImg").attr("src",val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: tit,
		area: ['650px', '500px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});


}

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            name: null,
			authStatus: null
        },
		showList: true,
		title: null,
		dict: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			$("#showImg").removeAttr("src");
			vm.showList = false;
			vm.title = "新增";
			vm.dict = {};
		},
		update: function (event) {
			$("#showImg").removeAttr("src");
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "审核";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dict.id == null ? "sys/product/add" : "sys/product/edit";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dict),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/product/remove",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/product/"+id, function(r){
                vm.dict = r.data;
            });
		},
		reload: function (event) {
			$("#showImg").removeAttr("src");
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{"keyWord": vm.q.keyWord,"status":vm.q.status},
                page:1
            }).trigger("reloadGrid");
		},
		fileUpload:function () {
			var formData = new FormData();
			formData.append('file', $('#file')[0].files[0]);
			$.ajax({
				method: 'POST',
				url: baseURL + '/sys/upload/img',
				data: formData,
				cache: false,
				processData: false,
				contentType: false
			}).done(function(res) {
				url = res.data;
				if (url) {
					$("#imageId").removeAttr("src");
					vm.dict.urlSmall = url;
					$("#imageId").attr("src", url);
				}
			}).fail(function(res) {});
		}
	}
});